package com.huishu.commons.pojo;

import com.huishu.commons.enums.ResponseEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
 * 接口响应结果实体
 *
 * @author ljj
 * @date 2021/4/10
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
public class Result<T> {

    /**
     * 状态
     */
    private Boolean status;

    /**
     * 消息
     */
    private String message;

    /**
     * 响应编码
     */
    private String responseCode;

    /**
     * 结果对象
     */
    private T entry;

    public Result(String code, String msg, T data) {
        this.responseCode = code;
        this.message = msg;
        this.entry = data;
    }


    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setStatus(true);
        result.setResponseCode(ResponseEnum.SUCCESS.getCode());
        result.setMessage(ResponseEnum.SUCCESS.getMessage());
        return result;
    }

    public static <T> Result<T> success(T entry) {
        Result<T> result = Result.success();
        result.setEntry(entry);
        return result;
    }

    public static <T> Result<T> success(T entry, String message) {
        Result<T> result = Result.success();
        result.setEntry(entry);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> fail() {
        Result<T> result = new Result<>();
        result.setStatus(false);
        result.setResponseCode(ResponseEnum.FAIL.getCode());
        result.setMessage(ResponseEnum.FAIL.getMessage());
        return result;
    }

    public static <T> Result<T> fail(ResponseEnum responseEnum) {
        Result<T> result = new Result<>();
        result.setStatus(false);
        result.setMessage(responseEnum.getMessage());
        result.setResponseCode(responseEnum.getCode());
        return result;
    }

    public static <T> Result<T> success(ResponseEnum responseEnum) {
        Result<T> result = Result.success();
        result.setMessage(responseEnum.getMessage());
        result.setResponseCode(responseEnum.getCode());
        return result;
    }

    public static <T> Result<T> fail(ResponseEnum responseEnum, String msg) {
        Result<T> result = fail(responseEnum);
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> fail(String msg) {
        Result<T> result = fail();
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> fail(ResponseEnum responseEnum, T entry) {
        Result<T> result = fail(responseEnum);
        result.setEntry(entry);
        return result;
    }

    /**
     * 自定义
     *
     * @param code
     * @param msg
     * @return
     */
    public static Result custom(String code, String msg) {
        return new Result(code, msg, null);
    }

}
